import "package:flutter/material.dart";
import "package:myapp/_views/dynamic/type.dart";

import "UPIdentifyWidget.dart";
import "type.dart";

// 推荐直播
class LiveRecommendItem {
  UPInfo upInfo;
  LiveContentItem liveContentItem;
  LiveRecommendItem({
    required this.upInfo,
    required this.liveContentItem,
  });
}

// 直播-推荐列表
class LiveRecommendListWidget extends StatelessWidget {
  final List<LiveRecommendItem> liveRecommendList = [
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '是寒酥丫',
        avatar: './images/tab_1.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/1.jpg",
        viewCount: "50",
        title: "有一个好友正在观看",
        partition: '虚拟日常',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '帕里_Paryi',
        avatar: './images/tab_3.jpg',
        isSubscribed: true,
        dynamicUPInfoAuthentication: DynamicUPInfoAuthentication(
          type: 'UP',
          name: 'bilibili 知名UP主',
        ),
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/1.jpg",
        viewCount: "6538",
        title: "学3d",
        partition: '虚拟日常',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
    LiveRecommendItem(
      upInfo: UPInfo(
        name: '大奇sama二号机',
        avatar: './images/tab_2.jpg',
      ),
      liveContentItem: LiveContentItem(
        coverUrl: "./images/3.jpg",
        viewCount: "858",
        title: "画新的儿童绘本",
        partition: '手工绘画',
      ),
    ),
  ];
  LiveRecommendListWidget({super.key});
  @override
  Widget build(BuildContext context) {
    int column = 2;
    List<List<LiveRecommendItem>> rowList = [];
    List<LiveRecommendItem> row = [];
    for (int i = 0; i < liveRecommendList.length; i++) {
      // 先插入后判断，
      row.add(liveRecommendList[i]);
      // 如果当前已经是最后一个，但最后一行元素不足2个，舍弃
      if (row.length == column) {
        rowList.add(row);
        row = [];
      }
    }
    double spacing = 5;

    return SliverPadding(
      padding: const EdgeInsets.only(left: 6, right: 6),
      sliver: SliverList.builder(
        itemCount: rowList.length,
        itemBuilder: (context, index) {
          List<Widget> children = [];
          for (int i = 0; i < rowList[index].length; i++) {
            children.add(
              Expanded(
                flex: 1,
                child: Padding(
                  padding: EdgeInsets.only(
                      left: i == 0 ? 0 : spacing / 2,
                      right: i == rowList[index].length - 1 ? 0 : spacing / 2),
                  child: LiveRecommendItemWidget(
                      liveRecommendItem: rowList[index][i]),
                ),
              ),
            );
          }
          return IntrinsicHeight(
            child: Padding(
              padding: EdgeInsets.only(bottom: spacing),
              child: Row(children: children),
            ),
          );
        },
      ),
    );
  }
}

class LiveRecommendItemWidget extends StatelessWidget {
  final LiveRecommendItem liveRecommendItem;
  const LiveRecommendItemWidget({
    super.key,
    required this.liveRecommendItem,
  });
  @override
  Widget build(BuildContext context) {
    LiveContentItem liveContentItem = liveRecommendItem.liveContentItem;
    UPInfo upInfo = liveRecommendItem.upInfo;
    return ClipRRect(
      borderRadius: BorderRadius.circular(4),
      child: Container(
        decoration: const BoxDecoration(
          color: Colors.white,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            AspectRatio(
              aspectRatio: 16 / 9,
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  Image.asset(
                    liveContentItem.coverUrl,
                    width: double.infinity,
                    height: double.infinity,
                    fit: BoxFit.cover,
                  ),
                  Container(
                    height: 24,
                    width: double.infinity,
                    padding: const EdgeInsets.only(left: 4, right: 4),
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        begin: Alignment.bottomCenter,
                        end: Alignment.topCenter,
                        colors: [
                          Color.fromARGB(120, 0, 0, 0),
                          Color.fromARGB(80, 0, 0, 0),
                          Color.fromARGB(40, 0, 0, 0),
                          Color.fromARGB(0, 0, 0, 0),
                        ],
                      ),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Row(
                          children: [
                            const Icon(
                              Icons.remove_red_eye_outlined,
                              color: Colors.white,
                              size: 14,
                            ),
                            Text(
                              liveContentItem.viewCount,
                              style: const TextStyle(
                                color: Colors.white,
                                fontSize: 10,
                              ),
                            )
                          ],
                        ),
                        liveContentItem.partition != null
                            ? Text(
                                liveContentItem.partition ?? '',
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 10,
                                ),
                              )
                            : const Row(
                                children: [
                                  Icon(
                                    Icons.bar_chart,
                                    color: Colors.white,
                                    size: 16,
                                  ),
                                  Text(
                                    '直播',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 10,
                                    ),
                                  )
                                ],
                              ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(10, 8, 0, 8),
              child: IntrinsicHeight(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 4),
                      child: Stack(
                        children: [
                          CircleAvatar(
                            radius: 17,
                            backgroundImage: AssetImage(upInfo.avatar),
                          ),
                          upInfo.dynamicUPInfoAuthentication != null
                              ? Positioned(
                                  right: -1,
                                  bottom: -1,
                                  child: UPIdentifyWidget(
                                    dynamicUPInfoAuthentication:
                                        upInfo.dynamicUPInfoAuthentication,
                                  ))
                              : const SizedBox.shrink(),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            liveContentItem.title,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: const TextStyle(fontSize: 14),
                          ),
                          Row(
                            children: [
                              upInfo.isSubscribed
                                  ? const Padding(
                                      padding: EdgeInsets.only(right: 2),
                                      child: Text(
                                        '已关注',
                                        style: TextStyle(
                                          color:
                                              Color.fromARGB(255, 255, 106, 0),
                                          fontSize: 12,
                                        ),
                                      ),
                                    )
                                  : const SizedBox.shrink(),
                              Expanded(
                                child: Text(
                                  upInfo.name,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: const TextStyle(
                                    color: Colors.black26,
                                    fontSize: 12,
                                  ),
                                ),
                              )
                            ],
                          ),
                        ],
                      ),
                    ),
                    const Padding(
                      padding: EdgeInsets.only(left: 2),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.more_vert,
                            size: 16,
                            color: Colors.black26,
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
